package com.ykt.controller;

import com.jfinal.aop.Before;
import com.ykt.common.constant.ParamNames;
import com.ykt.common.utils.Colls;
import com.ykt.common.utils.DateUtil;
import com.ykt.common.utils.ModelUtil;
import com.ykt.common.utils.UB;
import com.ykt.common.utils.page.PageData;
import com.ykt.common.utils.page.PageParam;
import com.ykt.converter.OrderConvert;
import com.ykt.model.Order;
import com.ykt.model.OrderItem;
import com.ykt.service.OrderService;
import com.ykt.validator.CommonValidator;
import com.ykt.vo.OrderVo;
import com.ykt.vo.ResultVo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户订单操作
 *
 * @author wing
 * @version 1.0.0
 */
public class OrderController extends BaseController {

    private OrderService orderService = enhance(OrderService.class.getName(), OrderService.class);


    /**
     * 商户查询订单
     */
    @Before({CommonValidator.ShopIdValidator.class, CommonValidator.OrderStatusValidator.class})
    public void find() {
        int shopId = UB.i(getPara("shopId"));
        String orderStatus = getPara("orderStatus");
        String serviceStatus = getPara("serviceStatus");

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put(Order.shop_id, shopId);
        paramMap.put(Order.status, orderStatus);
        paramMap.put(Order.service_status, serviceStatus);

        PageParam<Map<String, Object>> param = toPageParam(paramMap);
        PageData<Order> pd = orderService.pageFindByShopIdAndStatus(param);
        
        if(pd == null){
            this.returnJson(ResultVo.failure());
            return;
        }

        if(Colls.isEmpty(pd.getPageData())){
            this.returnJson(ResultVo.<String>success(null));
            return;
        }
        
        PageData<OrderVo> voPd = pd.convert(new ArrayList<OrderVo>());
        for (Order order : pd.getPageData()) {
            OrderVo vo = new OrderVo();
            vo.setId(order.getInt(Order.id));
            vo.setUserName(order.getStr(ParamNames.userName));
            vo.setAmount(ModelUtil.d(order, Order.amount));
            vo.setPayAmount(ModelUtil.d(order, Order.pay_amount));
            vo.setAgentFee(ModelUtil.d(order, Order.agent_fee));
            vo.setStatus(order.getStr(Order.status));
            vo.setServiceStatus(order.getStr(Order.service_status));
            vo.setPayTime(DateUtil.ymdhms2(order.getDate(Order.pay_time)));
            vo.setCreatedTime(DateUtil.ymdhms2(order.getDate(Order.created_time)));

            voPd.getPageData().add(vo);
        }

        this.returnJson(ResultVo.success(voPd));
    }


    /**
     * 订单详情
     */
    @Before(CommonValidator.OrderIdValidator.class)
    public void detail() {
        int orderId = UB.i(getPara("orderId"), -1);

        Order order = orderService.findById(orderId);
        OrderVo orderVo = OrderConvert.convert(order);

        List<OrderItem> orderItems = orderService.findOrderItems(orderId);
        if (orderVo != null) {
            orderVo.setGoods(OrderConvert.convertOderItemList(orderItems));
        }

        this.returnJson(ResultVo.success(orderVo));
    }


    /**
     * 商户订单状态修改
     */
    @Before({CommonValidator.OrderIdValidator.class, CommonValidator.OrderStatusValidator.class})
    public void modifyStatus() {
        int orderId = UB.i(getPara("orderId"));
        String orderStatus = getPara("orderStatus");

        orderService.updateStatus(orderId, orderStatus);

        this.returnJson(ResultVo.success());
    }


    /**
     * 订单状态轮询
     */
    @Before(CommonValidator.ShopIdValidator.class)
    public void polling() {
        int shopId = UB.i(getPara("shopId"), -1);

        List<Order> data = orderService.findByMerchantUnconfirmed(shopId);
        int size = (data == null) ? 0 : data.size();

        Map<String, Boolean> map = new HashMap<>();
        map.put("hasUnConfirmOrder", (size > 0));

        this.returnJson(ResultVo.success(map));
    }
}
